﻿using Sunny.UI;
using System.Data;

namespace Ycjw.PipeLine.Map.Basic.Results
{
    public class ResultWindowWithBarChart : ResultWindowWithChart
    {
        protected override void AddOptionSeries(DataTable dt)
        {
            foreach(DataRow dr in dt.Rows)
            {
                var color = m_Colors[dt.Rows.IndexOf(dr)];
                var series = new UIBarSeries
                {
                    Name = dr[0].ToString()
                };
                for (int i = 1;i < dr.ItemArray.Length; i++)
                {
                    double.TryParse(dr[i].ToString(), out double val);
                    series.AddData(val, color);
                }
                ((UIBarOption)m_UIOption).AddSeries(series);
            }
        }

        protected override void AddXAxis(DataTable dt)
        {
            var option = m_UIOption as UIBarOption;
            option.XAxis.Scale = true;
            option.XAxis.MaxAuto = true;
            option.XAxis.MinAuto = true;
            var colIndex = 0;
            foreach(DataColumn colum in dt.Columns)
            {
                if (colIndex++ == 0) continue;
                option.XAxis.Data.Add(colum.ColumnName);
            }

            if (option.XAxis.Data.Count > 5)
            {
                option.XAxis.AxisLabel.Angle = 45;
            }
            else
            {
                option.XAxis.AxisLabel.Angle = 0;
            }
        }
        protected override void SetLegendItem(DataTable dt)
        {
            foreach(DataRow dr in dt.Rows)
            {
                ((UIBarOption)m_UIOption).Legend.AddData(dr[0].ToString(), m_Colors[dt.Rows.IndexOf(dr)]);
            }
        }
        protected override void CreateChart()
        {
            this.m_UIChart = new UIBarChart();
            m_UIOption = new UIBarOption();
        }
        protected override void SetOptionEx()
        {
            ((UIBarOption)m_UIOption).XAxis.Name = SetXAxisName();
            ((UIBarOption)m_UIOption).YAxis.Name = SetYAxisName();
            ((UIBarOption)m_UIOption).Grid.Left = 80;
        }
    }
}
